1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
|
#include <bits/stdc++.h> #define rep(i, x, y) for (int i = x; i <= y; i++) using namespace std;
typedef long long ll; const int mod = 1e9 + 7, N = 1e6 + 10; ll T, K, l, r, f[2][3][65], bit2[65];
ll sub(ll a, ll b) { return (a - b + mod) % mod; } ll add(ll a, ll b) { return (a + b + mod) % mod; }
void init(int n) { bit2[0] = 1; rep(i, 1, n) bit2[i] = bit2[i - 1] * 2 % mod; f[0][0][0] = 3; f[0][1][0] = 1; f[0][2][0] = 1; rep(i, 1, n) { f[0][0][i] = (f[0][0][i - 1] + 2 * f[0][0][i - 1]) % mod; f[0][1][i] = (f[0][1][i - 1] + 2 * f[0][1][i - 1] % mod + bit2[i] * f[0][0][i - 1] % mod) % mod; f[0][2][i] = (f[0][2][i - 1] + 2 * f[0][2][i - 1] % mod + 2 * f[0][1][i - 1] % mod * bit2[i] % mod + bit2[i] * bit2[i] % mod * f[0][0][i - 1] % mod) % mod; } }
ll calc(ll n, int K) { if (!n) return 0; ll cnt = 0, x = n; while (x) x >>= 1, ++cnt; f[1][0][0] = (n & 1) ? 3 : 1; f[1][1][0] = (n & 1) ? 1 : 0; f[1][2][0] = (n & 1) ? 1 : 0; rep(i, 1, cnt) { if ((n >> i) & 1) { f[1][0][i] = (f[0][0][i - 1] + 2 * f[1][0][i - 1] % mod) % mod; f[1][1][i] = (f[0][1][i - 1] + 2 * f[1][1][i - 1] % mod + bit2[i] * f[1][0][i - 1] % mod) % mod; f[1][2][i] = (f[0][2][i - 1] + 2 * f[1][2][i - 1] % mod + 2 * f[1][1][i - 1] % mod * bit2[i] % mod + bit2[i] * bit2[i] % mod * f[1][0][i - 1] % mod) % mod; } else { rep(j, 0, 2) f[1][j][i] = f[1][j][i - 1]; } } return f[1][K][cnt]; }
int main() { cin >> T >> K; init(60); while (T--) { scanf("%lld%lld", &l, &r); printf("%lld\n", sub(calc(r, K), calc(l - 1, K))); } return 0; }
|